import {imageUrl} from "../../utils/imageutil.js"
import {is_auth} from "../../utils/util"
let app = getApp()
Page({
  data: {
    imageUrl: imageUrl,
    list2_index:'',//服务规格切换
    good_id:'',//分类ID
    store_id:'',//店铺ID
    logo:'',//顶部图片
    swiperCurrent:'',//图片数量
    name:'',//名称
    sales:'',//已售
    content:'',//详情
    myDefaultAddress:'',//地址详细信息
    addressid:'',//地址ID
    list:[],//服务规格
    spec:'',//规格描述
    combination_id:'',//服务规格类ID
    assess:[],//评论列表
    flag:true,
    page:1,
    money:'',//规格价钱
    nodes:'',//商品详情
    lists:[
      {src:imageUrl+'/matron/advance.png',text:'1.预付下单',time:'预定服务时间'},
      {src:imageUrl+'/matron/payment.png',text:'2.确认上门',time:'提前电话沟通'},
      {src:imageUrl+'/matron/quotation.png',text:'3.报价服务',time:'按价目表收费'},
      {src:imageUrl+'/matron/todoor.png',text:'4.验收付款',time:'确保服务完成'}
    ],
    one:false,
    two:false,
    good_info:null,
    swiperCurrent: 0,
    hideModal: true,
    need_title:'',
    selectIndex:-1,
    needcurr:-1,
    selectchildIndex:-1,
    needchildcurr:-1,
    acreagecurr:-1,
    acreageList:[],
    id:-1//地址ID
  },
  onLoad(options) {
    this.setData({
      good_id: options.good_id,
    })
    this.getGoodInfo()
    this.getList()
  },
  onShow(){
    wx.getStorageSync('id')
    this.MyAddressInfo()
  },
  MyAddressInfo(){//选择地址
      getApp().globalData.post({
        data:{
          do:"MyAddressInfo",
          id:wx.getStorageSync('id')
        }
      }).then(res=>{
          this.setData({
            myDefaultAddress:res.data,
            addressid:res.data.id
          })
      })   
  },
  // getMyDefaultAddress(){//默认地址
  //   getApp().globalData.post({
  //     data:{
  //       do:"MyDefaultAddress",
  //       user_id:wx.getStorageSync('userInfo').id
  //     }
  //   }).then(res=>{
  //     if(res.data==false){
  //       this.setData({
  //         myDefaultAddress:null,
  //         addressid:''
  //       })
  //     }else{
  //       this.setData({
  //         myDefaultAddress:res.data,
  //         addressid:res.data.id
  //       })
  //     }
  //   })
  // },
  getGoodInfo(){//分类信息
    getApp().globalData.post({
      data:{
        do:"GoodInfo",
        good_id:this.data.good_id
      }
    }).then(res=>{
        var list = res.data;
          this.setData({
            logo:list.logo,
            name:list.name,
            sales:list.sales,
            content:list.content,
            store_id:list.store_id,
            store_name:list.store_name,
            store_level:list.store_level,
            store_logo:list.store_logo,
            nodes:list.details,
            spec: list.spec.length>0?(list.spec[0].spec_val[0].spec_val_name):list.name,
            money:list.spec.length>0?(list.spec[0].spec_val[0].spec_val_money):list.money,
            combination_id:list.spec.length>0?(list.spec[0].spec_val[0].combination_id):list.id,
           })
           if(res.data.spec.length==1){
             this.setData({
              good_info:{
                description:res.data.description
              },
              list:res.data.spec[0].spec_val,
              one:true,
              two:false
             })
           }else if(res.data.spec.length==0){
            this.setData({
              good_info:res.data,
              one:false,
              two:false,
              combination_id:''
            })
           }else{
            this.setData({
              good_info:res.data,
              one:false,
              two:true
            })
           }
    })
  },
  changelist_index(e){//规格选择
    console.log(e)
    this.setData({
      list2_index: e.currentTarget.dataset.index,
      money:e.currentTarget.dataset.money,
      combination_id:e.currentTarget.dataset.combination_id,
      spec:e.currentTarget.dataset.name
    })
  },
  changeSwiper(e) {//logo图选择
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  to_location(){//选择地址
    wx.navigateTo({
      url: '/pages/my/address?type='+1+'&good_id='+this.data.good_id,
    })
  },
  to_StoreInfo(e){
    wx.navigateTo({
      url: '../housekeeping/index?store_id='+this.data.store_id,
    })
  },
  to_submit(){//提交订单
    if(!is_auth(true)){
      return false
    }
    if(this.data.two==true&&this.data.selectIndex==-1){
      wx.showToast({
        title: '请选择规格',
        icon:'none'
      })
      return false
    } if(this.data.myDefaultAddress.area==''){
      wx.showToast({
        title: '请选择地址',
        icon:'none'
      })
      return false
    }
    getApp().globalData.post({
        data:{
          do: 'AddCar',
          good_id:this.data.good_id,//商品ID
          user_id:wx.getStorageSync('userInfo').id,//用户ID
          store_id:this.data.store_id,//店铺ID
          addressid:this.data.addressid,//地址ID
          combination_id:this.data.combination_id,//服务规格组合ID
          user_name:this.data.myDefaultAddress.user_name,//用户姓名
          tel:this.data.myDefaultAddress.tel,//手机号
          lat:this.data.myDefaultAddress.lat,//纬度
          lng:this.data.myDefaultAddress.lng,//经度
          area_code:this.data.myDefaultAddress.area_code,
          spec:this.data.spec,//规格描述
          num:1,//数量
          money:this.data.money,//价格
          area:this.data.myDefaultAddress.area,//省市区
          address:this.data.myDefaultAddress.address,//详细地址
          doorplate:this.data.myDefaultAddress.doorplate,//门牌号
        }
      }).then(res=>{
        console.log(res.data)
        wx.navigateTo({
          url: '../projects/submit?store_id='+this.data.store_id,
        })
      })
  },
  onReachBottom(){
    this.getList()
  },
  getList(){//评论列表
    if(!this.data.flag){
      return
    }
    this.setData({
      flag:false
    })
    getApp().globalData.post({
      data:{
        do:"GoodAssessList",
        good_id:this.data.good_id,
        type:'1', //类型1好评,2差评
        page:this.data.page, //页数
        pagesize:'10' //条数
      }
    }).then(res=>{
      if(res.data.assess.length==0){
        this.setData({
          flag:false
        })
      }
      let arr = this.data.assess.concat(res.data.assess);
        this.setData({
          flag:true,
          page:this.data.page+1,
          assess: arr
        })  
    })
  },
  openTelephone(){
    wx.showActionSheet({
      itemList: ['4008726866'],
      success (res) {
        console.log(res.tapIndex)
        wx.showModal({
          title:'是否拨打客服电话',
          content:'4008726866',
          success (res) {
            if (res.confirm) {
              wx.makePhoneCall({
                phoneNumber: '4008726866'
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    })
  },
  // 多规格2
  needchang(e){
    this.setData({
      selectIndex:e.currentTarget.dataset.selectindex,
      needcurr:e.currentTarget.dataset.id,
    })
    if(this.data.selectIndex==-1||this.data.selectchildIndex==-1){
      return false
    }
    this.getMoney(this.data.good_info.spec[0].spec_val[this.data.selectIndex].spec_val_name+','+this.data.good_info.spec[1].spec_val[this.data.selectchildIndex].spec_val_name)
  },
  needchildchang(e){
    this.setData({
      selectchildIndex:e.currentTarget.dataset.selectindex,
      needchildcurr:e.currentTarget.dataset.id,
    })
    if(this.data.selectIndex==-1||this.data.selectchildIndex==-1){
      return false
    }
    this.getMoney(this.data.good_info.spec[0].spec_val[this.data.selectIndex].spec_val_name+','+this.data.good_info.spec[1].spec_val[this.data.selectchildIndex].spec_val_name)
  },
  getMoney(combination){
    if(this.data.selectIndex!=-1&&this.data.selectchildIndex!=-1){
      app.globalData.post({
        data:{
          do: 'GgZh',
          combination: combination,
          good_id: this.data.good_id
        }
      }).then(res=>{
        console.log(res)
        this.setData({
           money:res.data.wm_money,
           combination_id:res.data.id,
           combination:res.data.combination
        })
      })
    }
  },
  need_next(){
    if(this.data.needcurr==-1){
      wx.showToast({
        title: '请选择'+this.data.good_info.spec[0].spec_name,
        icon:'none'
      })
    }else{
      if(this.data.needchildcurr==-1){
        wx.showToast({
          title: '请选择'+this.data.good_info.spec[1].spec_name,
          icon:'none'
        })
      }else{
        this.setData({
          need_title:this.data.combination
        })
        this.setData({
          spec:this.data.combination
        })
        this.hideModal()
      }
    }
  },
  // 显示遮罩层 
  showModal: function() {
    var that = this;
    that.setData({
      hideModal: false
    })
    var animation = wx.createAnimation({
      duration: 600, //动画的持续时间 默认400ms 数值越大，动画越慢 数值越小，动画越快 
      timingFunction: 'ease', //动画的效果 默认值是linear 
    })
    this.animation = animation
    setTimeout(function() {
      that.fadeIn(); //调用显示动画 
    }, 200)
  },
  // 隐藏遮罩层 
  hideModal: function() {
    var that = this;
    var animation = wx.createAnimation({
      duration: 800, //动画的持续时间 默认400ms 数值越大，动画越慢 数值越小，动画越快 
      timingFunction: 'ease', //动画的效果 默认值是linear 
    })
    this.animation = animation
    that.fadeDown(); //调用隐藏动画 
    setTimeout(function() {
      that.setData({
        hideModal: true
      })
    }, 720) //先执行下滑动画，再隐藏模块 
  },
  //动画集 
  fadeIn: function() {
    this.animation.translateY(0).step()
    this.setData({
      animationData: this.animation.export() //动画实例的export方法导出动画数据传递给组件的animation属性 
    })
  },
  fadeDown: function() {
    this.animation.translateY(300).step()
    this.setData({
      animationData: this.animation.export(),
    })
  },
  onShareAppMessage(){
    let url='/pages/all/make?good_id='+this.data.good_id
    url = encodeURIComponent(url)
    return{
      path:'/pages/index/blank?to_id='+wx.getStorageSync('userInfo').id+'&tourl='+url,
      title:this.data.name,
      imageUrl:this.data.logo
    }
  },
})